package com.syezon.wifikey.wifi.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.syezon.android.base.utils.ToastUtils;
import com.syezon.wifikey.R;
import com.syezon.wifikey.home.MainActivity;
import com.syezon.wifikey.wifi.WiFiConnectDialog;
import com.syezon.wifikey.wifi.info.WifiInfo;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/9/11.
 */

public class EnterPwdDialog extends Dialog {

    @BindView(R.id.tv_ssid)
    TextView mTvSsid;
    @BindView(R.id.edit_pwd)
    EditText mEditPwd;
    @BindView(R.id.fl_show_pwd)
    FrameLayout mFlShowPwd;
    @BindView(R.id.iv_show_pwd)
    ImageView mIvShowPwd;
    @BindView(R.id.view_btm_line)
    View mViewBtmLine;
    @BindView(R.id.iv_close)
    ImageView mIvClose;
    @BindView(R.id.tv_confirm)
    TextView mTvConfirm;
    private WifiInfo wifiInfo;
    private boolean mPwdError;

    public static void enterPwd(Context context, WifiInfo wifiInfo, boolean pwdError) {
        EnterPwdDialog enterPwdDialog = new EnterPwdDialog(context);
        enterPwdDialog.setWifiInfo(wifiInfo);
        enterPwdDialog.setPwdError(pwdError);
        enterPwdDialog.show();
    }

    public EnterPwdDialog(@NonNull Context context) {
        this(context, 0);
    }

    public EnterPwdDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉title，在有的手机dialog顶部会出现空白
        setContentView(View.inflate(context, R.layout.dialog_enter_pwd, null));
        ButterKnife.bind(this);
        // 设置弹出窗体的宽和高
        setCanceledOnTouchOutside(false);
        setCancelable(false);
        Window window = getWindow();
        window.setLayout(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        mEditPwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                int textSum = editable.toString().length();
                mViewBtmLine.setBackgroundResource(R.color.colorPrimary);
                if (textSum >= 8) {
                    mTvConfirm.setPressed(true);
                } else {
                    mTvConfirm.setPressed(false);
                }
            }
        });
    }

    public void setWifiInfo(WifiInfo wifiInfo) {
        this.wifiInfo = wifiInfo;
        mTvSsid.setText(wifiInfo.getSsid());
    }

    public void setPwdError(boolean pwdError) {
        this.mPwdError = pwdError;
        if (mPwdError) {
            mViewBtmLine.setBackgroundColor(Color.RED);
            mEditPwd.setHint("输入密码错误");
            mEditPwd.setHintTextColor(Color.RED);
        } else {
            mEditPwd.setHint("");
            mEditPwd.setHintTextColor(Color.parseColor("#FF272727"));
            mViewBtmLine.setBackgroundResource(R.color.colorPrimary);
        }
    }

    @OnClick({R.id.fl_show_pwd, R.id.iv_close, R.id.tv_cancel, R.id.tv_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.fl_show_pwd:
                mIvShowPwd.setSelected(!mIvShowPwd.isSelected());
                int passwordLength = mEditPwd.getText().length();
                mEditPwd.setInputType(mIvShowPwd.isSelected() ?
                        (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) :
                        (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD));
                mEditPwd.setSelection(passwordLength);
                break;
            case R.id.iv_close:
                dismiss();
                break;
            case R.id.tv_cancel:
                dismiss();
                break;
            case R.id.tv_confirm:
                enterPwdConfirm();
                break;
        }
    }

    private void enterPwdConfirm() {
        String pwd = mEditPwd.getText().toString();
        if (pwd.length() < 8) {
            ToastUtils.showShort(R.string.pwd_short_tip);
        } else {
            wifiInfo.setPwd(pwd);
            WiFiConnectDialog.connect(getContext(), wifiInfo, new WiFiConnectDialog.ConnectCallBack() {
                @Override
                public void onSuccess() {
                    try {
                        Activity activity = ActivityUtils.getTopActivity();
                        if (activity != null && activity instanceof MainActivity) {
                            ((MainActivity) activity).showNewsTab(null);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onFailed() {

                }
            });
            dismiss();
        }
    }
}
